﻿using App.Common;
using App.Common.SMTP;
using App.IService;
using App.Model.DTO.User;
using App.WebAPI.Utilty.ApiResult;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace App.WebAPI.Controllers
{
    [Route("[controller]/[Action]")]
    [ApiController]
    public class EmailController : ControllerBase
    {
        private readonly EmailService _emailService;
        private readonly IUserInfoService _iUserInfoService;

        public EmailController(EmailService emailService, IUserInfoService iUserInfoService)
        {
            _emailService = emailService;
            _iUserInfoService = iUserInfoService;
        }

        /// <summary>
        /// 邮箱登录验证码
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> SendEmailValidateCode(EmailLoginSendRequestInput req)
        {
            if (string.IsNullOrWhiteSpace(req.Email))
            {
                return ApiResultHelper.Error("邮箱不能为空");
            }

            var user = await _iUserInfoService.FindAsync(c => c.Email == req.Email);
            if (user == null)
            {
                return ApiResultHelper.Error("邮箱不存在");
            }

            // 生成验证码
            var validateCode = new Random().Next(100000, 999999).ToString();
            string emailTemplate = @"
                            <!DOCTYPE html>
                            <html lang=""en"">
                            <head>
                                <meta charset=""UTF-8"">
                                <meta name=""viewport""
                                      content=""width=device-width, initial-scale=1.0"">
                                <title>Document</title>
                                <style>
                                    body {
                                        font-family: Arial, sans-serif;
                                        background-color: #f5f5f5;
                                        margin: 0;
                                        padding: 0;
                                    }

                                    .container {
                                        max-width: 600px;
                                        margin: 50px auto;
                                        background-color: #ffffff;
                                        padding: 30px;
                                        border-radius: 5px;
                                        box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
                                    }

                                    h2 {
                                        color: #333333;
                                        text-align: center;
                                        margin-top: 0;
                                    }

                                    p {
                                        color: #666666;
                                        line-height: 1.6;
                                    }

                                    .code {
                                        font-size: 24px;
                                        font-weight: bold;
                                        color: #007BFF;
                                        margin: 20px 0;
                                        text-align: center;
                                    }

                                    .notice {
                                        color: #999999;
                                        text-align: center;
                                    }
                                    .email {
                                        color: #007BFF;
                                    }
                                </style>
                            </head>

                            <body>
                                <div class=""container"">
                                    <h2>欢迎使用邮箱登录验证</h2>
                                    <p>您好，<span class=""email"">{{Email}}</span></p>
                                    <p>感谢您选择使用我们的服务。以下是您的邮箱登录验证码，请查收：</p>

                                    <div class=""code"">
                                        <span id=""validateCode"">{{VALIDATE_CODE}}</span>
                                    </div>

                                    <p class=""notice"">请注意，此验证码有效期为5分钟，请在有效期内完成操作。为了保障您的账户安全，请勿向任何人透露此验证码。</p>

                                    <p>如果您没有请求此验证码，请忽略此邮件，可能是有人误输入了您的邮箱地址。</p>

                                    <p>祝您使用愉快！</p>

                                    <p>此致<br>梦博客团队</p>
                                </div>
                            </body>

                            </html>";
            // 发送验证码邮件
            emailTemplate = emailTemplate.Replace("{{VALIDATE_CODE}}", validateCode);
            emailTemplate = emailTemplate.Replace("{{Email}}", req.Email);
            var subject = "邮箱登录验证码";
            var message = emailTemplate;
            MemoryHelper.SetMemory(validateCode, validateCode, 5);
            await _emailService.SendEmailAsync(req.Email, subject, message);
            return ApiResultHelper.Success("验证码已发送", true);
        }
        /// <summary>
        /// 邮箱注册验证码
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> EmailRegisterSendEmailValidateCode(EmailRegisterValidateCodeRequestInput req)
        {
            if (string.IsNullOrWhiteSpace(req.Email))
            {
                return ApiResultHelper.Error("邮箱不能为空");
            }
            // 生成验证码
            var validateCode = new Random().Next(100000, 999999).ToString();
            var registerValidateCode = validateCode + "register";
            string emailTemplate = @"
                            <!DOCTYPE html>
                            <html lang=""en"">
                            <head>
                                <meta charset=""UTF-8"">
                                <meta name=""viewport""
                                      content=""width=device-width, initial-scale=1.0"">
                                <title>Document</title>
                                <style>
                                    body {
                                        font-family: Arial, sans-serif;
                                        background-color: #f5f5f5;
                                        margin: 0;
                                        padding: 0;
                                    }

                                    .container {
                                        max-width: 600px;
                                        margin: 50px auto;
                                        background-color: #ffffff;
                                        padding: 30px;
                                        border-radius: 5px;
                                        box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
                                    }

                                    h2 {
                                        color: #333333;
                                        text-align: center;
                                        margin-top: 0;
                                    }

                                    p {
                                        color: #666666;
                                        line-height: 1.6;
                                    }

                                    .code {
                                        font-size: 24px;
                                        font-weight: bold;
                                        color: #007BFF;
                                        margin: 20px 0;
                                        text-align: center;
                                    }

                                    .notice {
                                        color: #999999;
                                        text-align: center;
                                    }
                                    .email {
                                        color: #007BFF;
                                    }
                                </style>
                            </head>

                            <body>
                                <div class=""container"">
                                    <h2>欢迎使用邮箱注册验证</h2>
                                    <p>您好，<span class=""email"">{{Email}}</span></p>
                                    <p>感谢您选择使用我们的服务。以下是您的邮箱注册验证码，请查收：</p>

                                    <div class=""code"">
                                        <span id=""validateCode"">{{VALIDATE_CODE}}</span>
                                    </div>

                                    <p class=""notice"">请注意，此验证码有效期为5分钟，请在有效期内完成操作。为了保障您的账户安全，请勿向任何人透露此验证码。</p>

                                    <p>如果您没有请求此验证码，请忽略此邮件，可能是有人误输入了您的邮箱地址。</p>

                                    <p>祝您使用愉快！</p>

                                    <p>此致<br>梦博客团队</p>
                                </div>
                            </body>

                            </html>";
            // 发送验证码邮件
            emailTemplate = emailTemplate.Replace("{{VALIDATE_CODE}}", validateCode);
            emailTemplate = emailTemplate.Replace("{{Email}}", req.Email);
            var subject = "邮箱注册验证码";
            var message = emailTemplate;
            MemoryHelper.SetMemory(registerValidateCode, validateCode, 5);
            await _emailService.SendEmailAsync(req.Email, subject, message);
            return ApiResultHelper.Success("验证码已发送", true);
        }
        /// <summary>
        /// 重置密码邮箱验证码
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> EmailResetPwdSendEmailValidateCode(ResetPwdEmailValidateCodeRequestInput req)
        {
            if (string.IsNullOrWhiteSpace(req.Email))
            {
                return ApiResultHelper.Error("邮箱不能为空");
            }
            // 生成验证码
            var validateCode = new Random().Next(100000, 999999).ToString();
            var registerValidateCode = validateCode + "resetPwd";
            string emailTemplate = @"
                            <!DOCTYPE html>
                            <html lang=""en"">
                            <head>
                                <meta charset=""UTF-8"">
                                <meta name=""viewport""
                                      content=""width=device-width, initial-scale=1.0"">
                                <title>Document</title>
                                <style>
                                    body {
                                        font-family: Arial, sans-serif;
                                        background-color: #f5f5f5;
                                        margin: 0;
                                        padding: 0;
                                    }

                                    .container {
                                        max-width: 600px;
                                        margin: 50px auto;
                                        background-color: #ffffff;
                                        padding: 30px;
                                        border-radius: 5px;
                                        box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
                                    }

                                    h2 {
                                        color: #333333;
                                        text-align: center;
                                        margin-top: 0;
                                    }

                                    p {
                                        color: #666666;
                                        line-height: 1.6;
                                    }

                                    .code {
                                        font-size: 24px;
                                        font-weight: bold;
                                        color: #007BFF;
                                        margin: 20px 0;
                                        text-align: center;
                                    }

                                    .notice {
                                        color: #999999;
                                        text-align: center;
                                    }
                                    .email {
                                        color: #007BFF;
                                    }
                                </style>
                            </head>

                            <body>
                                <div class=""container"">
                                    <h2>欢迎使用邮箱重置密码验证</h2>
                                    <p>您好，<span class=""email"">{{Email}}</span></p>
                                    <p>感谢您选择使用我们的服务。以下是您的邮箱重置密码验证码，请查收：</p>

                                    <div class=""code"">
                                        <span id=""validateCode"">{{VALIDATE_CODE}}</span>
                                    </div>

                                    <p class=""notice"">请注意，此验证码有效期为5分钟，请在有效期内完成操作。为了保障您的账户安全，请勿向任何人透露此验证码。</p>

                                    <p>如果您没有请求此验证码，请忽略此邮件，可能是有人误输入了您的邮箱地址。</p>

                                    <p>祝您使用愉快！</p>

                                    <p>此致<br>梦博客团队</p>
                                </div>
                            </body>

                            </html>";
            // 发送验证码邮件
            emailTemplate = emailTemplate.Replace("{{VALIDATE_CODE}}", validateCode);
            emailTemplate = emailTemplate.Replace("{{Email}}", req.Email);
            var subject = "邮箱重置密码验证码";
            var message = emailTemplate;
            MemoryHelper.SetMemory(registerValidateCode, validateCode, 5);
            await _emailService.SendEmailAsync(req.Email, subject, message);
            return ApiResultHelper.Success("验证码已发送", true);
        }
    }
}
